package biz

import (
	"time"

	"github.com/gin-gonic/gin"
	"gitlab1.moolink.net/trade/backend/gen"
	"lab.com/conf"
	"lab.com/consts"
	"lab.com/gen/entity"
	"lab.com/request"
)

type cooperationBiz struct {
}

// Cooperation  交流合作
var Cooperation = cooperationBiz{}

// List 交流合作列表
func (coo *cooperationBiz) List(c *gin.Context, req request.CooperationListReq) (out request.CooperationListRes, err error) {
	ctx := conf.Span(c, "biz cooperation list", nil)
	cooModel := conf.LabGen.Cooperation

	where := []gen.Condition{cooModel.Status.Eq(consts.CooNormal), cooModel.IsShow.Eq(consts.CooShow)}
	// if req.IsAll == consts.CooInHome {
	// 	where = append(where, cooModel.IsHome.Eq(consts.CooInHome))
	// }
	if req.Title != "" {
		where = append(where, cooModel.Title.Like("%"+req.Title+"%"))
	}
	if req.Type != 0 {
		where = append(where, cooModel.Type.Like(req.Type))
	}
	if req.StartTime != "" {
		if t, err := time.Parse("2006-01-02", req.StartTime); err != nil {
			return out, err
		} else {
			where = append(where, cooModel.PublishTime.Gte(t))
		}
	}
	if req.EndTime != "" {
		if t, err := time.Parse("2006-01-02", req.EndTime); err != nil {
			return out, err
		} else {
			where = append(where, cooModel.PublishTime.Lte(t))
		}
	}

	info, count, err := cooModel.WithContext(ctx).Where(where...).Order(cooModel.PublishTime).FindByPage(req.Offset, req.Size)
	if err != nil {
		return out, err
	}

	list := make([]request.CooperationBasicInfo, 0, len(info))
	for _, val := range info {
		list = append(list, request.CooperationBasicInfo{
			Id:          val.ID,
			Title:       val.Title,
			Abstract:    val.Abstract,
			Author:      val.Author,
			ImgPath:     val.ImgPath,
			Type:        val.Type,
			PublishTimt: val.PublishTime.Format("2006-01-02"),
		})
	}
	out.List = list
	out.Count = int(count)
	return out, nil
}

// Detail 交流合作详情
func (coo *cooperationBiz) Detail(c *gin.Context, req request.CooperationDetailReq) (out request.CooperationDetailRes, err error) {
	ctx := conf.Span(c, "biz cooperation detail", nil)
	cooModel := conf.LabGen.Cooperation

	info, err := cooModel.WithContext(ctx).Where(cooModel.ID.Eq(req.ID)).First()
	if err != nil {
		return out, err
	}

	out.Content = info.Content
	out.Id = info.ID
	out.Title = info.Title
	out.Abstract = info.Abstract
	out.Author = info.Author
	out.ImgPath = info.ImgPath
	out.Type = info.Type
	out.PublishTimt = info.PublishTime.Format("2006-01-02")
	out.IsShow = info.IsShow
	//out.IsHome = info.IsHome
	return out, nil
}

// Add 交流合作新增
func (coo *cooperationBiz) Add(c *gin.Context, req request.CooperationAddReq) (err error) {
	ctx := conf.Span(c, "biz cooperation add", nil)
	cooModel := conf.LabGen.Cooperation

	t, err := time.Parse("2006-01-02", req.PublishTime)
	if err != nil {
		return err
	}

	info := &entity.Cooperation{
		Title:       req.Title,
		Abstract:    req.Abstract,
		Author:      req.Author,
		Content:     req.Content,
		Type:        req.Type,
		PublishTime: t,
		IsShow:      req.IsShow,
		//IsHome:      req.IsHome,
		ImgPath:    req.ImgPath,
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}

	err = cooModel.WithContext(ctx).Create(info)
	return err
}

// Delete 交流合作删除
func (coo *cooperationBiz) Delete(c *gin.Context, req request.CooperationDeleteReq) (err error) {
	ctx := conf.Span(c, "biz cooperation delete", nil)
	cooModel := conf.LabGen.Cooperation

	info := &entity.Cooperation{
		Status:     consts.CooDelete,
		UpdateTime: time.Now(),
	}

	_, err = cooModel.WithContext(ctx).Select(cooModel.UpdateTime, cooModel.Status).Where(cooModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Edit 交流合作编辑
func (coo *cooperationBiz) Edit(c *gin.Context, req request.CooperationEditReq) (err error) {
	ctx := conf.Span(c, "biz cooperation edit", nil)
	cooModel := conf.LabGen.Cooperation

	t, err := time.Parse("2006-01-02", req.PublishTime)
	if err != nil {
		return err
	}

	info := &entity.Cooperation{
		Title:       req.Title,
		Abstract:    req.Abstract,
		Author:      req.Author,
		Content:     req.Content,
		Type:        req.Type,
		PublishTime: t,
		IsShow:      req.IsShow,
		//IsHome:      req.IsHome,
		ImgPath:    req.ImgPath,
		UpdateTime: time.Now(),
	}

	_, err = cooModel.WithContext(ctx).Select(cooModel.Title, cooModel.Abstract, cooModel.Author, cooModel.Content,
		cooModel.Type, cooModel.PublishTime, cooModel.IsShow, cooModel.ImgPath, cooModel.UpdateTime).
		Where(cooModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Show 交流合作展示
func (coo *cooperationBiz) Show(c *gin.Context, req request.CooperationShowReq) (err error) {
	ctx := conf.Span(c, "biz cooperation show", nil)
	cooModel := conf.LabGen.Cooperation

	info := &entity.Cooperation{
		IsShow:     req.IsShow,
		UpdateTime: time.Now(),
	}

	_, err = cooModel.WithContext(ctx).Select(cooModel.UpdateTime, cooModel.IsShow).Where(cooModel.ID.Eq(req.ID)).Updates(info)
	return err
}

// Home 交流合作首页展示
// func (coo *cooperationBiz) Home(c *gin.Context, req request.CooperationHomeReq) (err error) {
// 	ctx := conf.Span(c, "biz cooperation home", nil)
// 	cooModel := conf.LabGen.Cooperation

// 	if count, err := cooModel.WithContext(ctx).Where(cooModel.IsHome.Eq(consts.CooInHome), cooModel.IsShow.Eq(consts.CooShow),
// 		cooModel.Status.Eq(consts.CooNormal)).Count(); err != nil {
// 		return err
// 	} else if count > 8 {
// 		return errorcode.New(errorcode.ErrService, "首页展示个数超过限制！", nil)
// 	}

// 	info := &entity.Cooperation{
// 		IsHome:     req.IsHome,
// 		UpdateTime: time.Now(),
// 	}

// 	_, err = cooModel.WithContext(ctx).Select(cooModel.UpdateTime, cooModel.IsHome).Where(cooModel.ID.Eq(req.ID)).Updates(info)
// 	return err
// }

// List 交流合作列表
func (coo *cooperationBiz) ListInManage(c *gin.Context, req request.CooperationListInManageReq) (out request.CooperationListInManageRes, err error) {
	ctx := conf.Span(c, "biz cooperation list in manage", nil)
	cooModel := conf.LabGen.Cooperation

	where := []gen.Condition{cooModel.Status.Eq(consts.CooNormal)}
	if req.Title != "" {
		where = append(where, cooModel.Title.Like("%"+req.Title+"%"))
	}
	if req.Type != 0 {
		where = append(where, cooModel.Type.Like(req.Type))
	}
	if req.StartTime != "" {
		if t, err := time.Parse("2006-01-02", req.StartTime); err != nil {
			return out, err
		} else {
			where = append(where, cooModel.PublishTime.Gte(t))
		}
	}
	if req.EndTime != "" {
		if t, err := time.Parse("2006-01-02", req.EndTime); err != nil {
			return out, err
		} else {
			where = append(where, cooModel.PublishTime.Lte(t))
		}
	}

	info, count, err := cooModel.WithContext(ctx).Where(where...).Order(cooModel.PublishTime.Desc()).FindByPage(req.Offset, req.Size)
	if err != nil {
		return out, err
	}

	list := make([]request.CooperationInfo, 0, len(info))
	for _, val := range info {
		list = append(list, request.CooperationInfo{
			Id:          val.ID,
			Title:       val.Title,
			Abstract:    val.Abstract,
			Author:      val.Author,
			ImgPath:     val.ImgPath,
			Type:        val.Type,
			PublishTimt: val.PublishTime.Format("2006-01-02"),
			IsShow:      val.IsShow,
			//IsHome:      val.IsHome,
		})
	}
	out.List = list
	out.Count = int(count)
	return out, nil
}
